package net.expectx.micro.shop.api.controller;

import io.swagger.annotations.Api;
import net.expectx.micro.shop.api.common.ApiConstants;
import net.expectx.micro.shop.api.common.ApiResult;
import net.expectx.micro.shop.api.common.BaseController;
import net.expectx.micro.shop.dao.bean.ProductProduct;
import net.expectx.micro.shop.dao.bean.ProductProductExample;
import net.expectx.micro.shop.rpc.api.ProductBrandService;
import net.expectx.micro.shop.rpc.api.ProductCategoryService;
import net.expectx.micro.shop.rpc.api.ProductProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(value = "产品接口", description = "产品相关接口")
@Controller
@RequestMapping("product")
public class ProductController extends BaseController {
    @Autowired
    ProductProductService productProductService;
    @Autowired
    ProductBrandService productBrandService;
    @Autowired
    ProductCategoryService productCategoryService;
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<ApiResult> page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false,defaultValue = "-1",value = "productTypeId") int productTypeId) {
        Map<String, Object> result = new HashMap<>(16);
        ProductProductExample productProductExample = new ProductProductExample();
        /**
         * 创造条件
         */
        ProductProductExample.Criteria criteria = productProductExample.createCriteria();
        criteria.andIsDeletedEqualTo(ApiConstants.NO);
        //{id:1,name:"热销"},{id:2,name:"新品"},{id:3,name:"精品"},{id:4,name:"促销"}
        switch (productTypeId){
            case 1:
                criteria.andProductIsHotEqualTo(ApiConstants.YES);
                break;
            case 2:
                criteria.andProductIsNewEqualTo(ApiConstants.YES);
                break;
            case 3:
                criteria.andProductIsBestEqualTo(ApiConstants.YES);
                break;
            case 4:
                criteria.andProductIsPromoteEqualTo(ApiConstants.YES);
                break;
            default:
        }

        /**
         * 分页
         */
        productProductExample.setPageIndex(page);
        productProductExample.setPageSize(limit);
        /**
         * 排序
         */
        productProductExample.setOrderByClause("orders desc");

        List<ProductProduct> productProducts = productProductService.selectByExample(productProductExample);

        long total = productProductService.countByExample(productProductExample);

        result.put("count", total);
        result.put("msg", "");
        result.put("pages",total%limit==0?total/limit:(total/limit)+1);
        result.put("data", productProducts);
        return new ResponseEntity<>(new ApiResult(true,200,null,result), HttpStatus.OK);
    }


    @RequestMapping(value = "/details/{productSn}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<ApiResult> page(
            @PathVariable(value = "productSn") String productSn){
        ProductProductExample productProductExample = new ProductProductExample();
        /**
         * 创造条件
         */
        ProductProductExample.Criteria criteria = productProductExample.createCriteria();
        criteria.andIsDeletedEqualTo(ApiConstants.NO).andProductSnEqualTo(productSn);

        List<ProductProduct> productProducts = productProductService.selectByExampleWithBLOBs(productProductExample);

        ProductProduct productProduct=new ProductProduct();
        if (!productProducts.isEmpty()){
            productProduct=productProducts.get(0);
        }

        productProduct.setProductBrand(productBrandService.selectByPrimaryKey(productProduct.getProductBrandId()));
        productProduct.setProductCategory(productCategoryService.selectByPrimaryKey(productProduct.getProductCategoryId()));

        return new ResponseEntity<>(new ApiResult(true,200,null,productProduct), HttpStatus.OK);
    }
}
